﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormUpdatePayment1 : FormTemplet
    {
        private long _nPaymentID = -1L;

        private int _nCustomerID = -1;

        private string _sOperatorName = null;

        private DataSet _dsPayment = null;

        public FormUpdatePayment1()
        {
            this.InitializeComponent();
        }

        public FormUpdatePayment1(long nPaymentID) : this()
        {
            this._nPaymentID = nPaymentID;
            Payments payments = new Payments();
            try
            {
                this._dsPayment = payments.LoadPaymment(nPaymentID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            payments.Dispose();
            if (this._dsPayment != null)
            {
                if (this._dsPayment.Tables.Count == 3)
                {
                    if (this._dsPayment.Tables[0].Rows.Count > 0)
                    {
                        this.txtPaymentCode.Text = this._dsPayment.Tables[0].Rows[0]["编号"].ToString();
                        this._nCustomerID = Convert.ToInt32(this._dsPayment.Tables[0].Rows[0]["客户编号"]);
                        this.txtCustomer.Text = this._dsPayment.Tables[0].Rows[0]["客户名称"].ToString();
                        this.dtpPaymentDate.Value = DateTime.Parse(this._dsPayment.Tables[0].Rows[0]["付款日期"].ToString());
                        this.cbPaymentProject.Text = this._dsPayment.Tables[0].Rows[0]["款项"].ToString();
                        this.txtTotalValue.Text = this._dsPayment.Tables[0].Rows[0]["付款金额"].ToString();
                        this.cbWayOfPayment.Text = this._dsPayment.Tables[0].Rows[0]["付款方式"].ToString();
                        this.txtPaymentAddress.Text = this._dsPayment.Tables[0].Rows[0]["付款地址"].ToString();
                        this.txtRemark.Text = this._dsPayment.Tables[0].Rows[0]["备注"].ToString();
                        this._sOperatorName = this._dsPayment.Tables[0].Rows[0]["操作者"].ToString();
                        this.btnPreview.Enabled = true;
                        this.btnPrint.Enabled = true;
                        string text = "";
                        if (this._dsPayment.Tables[0].Rows[0]["结算标志"].ToString() == "S")
                        {
                            text += "结算的订单包含：";
                            for (int i = 0; i < this._dsPayment.Tables[1].Rows.Count; i++)
                            {
                                text = text + this._dsPayment.Tables[1].Rows[i]["订单号"].ToString() + ";";
                            }
                            text += "\r\n结算的付款单包含：";
                            for (int i = 0; i < this._dsPayment.Tables[2].Rows.Count; i++)
                            {
                                text = text + this._dsPayment.Tables[2].Rows[i]["编号"].ToString() + ";";
                            }
                        }
                        this.textBox1.Text = text;
                    }
                    else
                    {
                        MessageBox.Show("可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomer.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
            }
            this.cbPaymentProject.Focus();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtCustomer.Text.Trim() == "" || this._nCustomerID < 0)
            {
                MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelect.Focus();
            }
            else if (this.cbPaymentProject.Text.Trim().Length > 32 || this.cbPaymentProject.Text.Trim().Length < 1)
            {
                MessageBox.Show("款项 格式不正确，字符不能为空 且 小于等于32个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (!Regex.IsMatch(this.txtTotalValue.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,4}))$"))
            {
                MessageBox.Show("金额 格式不正确，此处应该为不为零的整数，如有小数，最大为4位小数", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.cbWayOfPayment.Text.Trim().Length > 16 || this.cbWayOfPayment.Text.Trim().Length < 1)
            {
                MessageBox.Show("付款方式 格式不正确，字符不能为空 且 小于等于16个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.txtPaymentAddress.Text.Trim().Length > 64)
            {
                MessageBox.Show("付款地址 格式不正确，应小于等于64个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.txtRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("付款地址 格式不正确，应小于等于64个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Payments payments = new Payments();
                bool flag = false;
                try
                {
                    flag = payments.UpdatePayment1(this._nPaymentID, this._nCustomerID, this.dtpPaymentDate.Value, this.cbPaymentProject.Text.Trim(), Convert.ToDouble(this.txtTotalValue.Text.Trim()), this.cbWayOfPayment.Text.Trim(), this.txtPaymentAddress.Text.Trim(), this.txtRemark.Text.Trim());
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                payments.Dispose();
                if (flag)
                {
                    MessageBox.Show("更改成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.ShowDialog();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            this.printDialog1.Document = this.printDocument1;
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            string text = "";
            if (this._dsPayment.Tables[0].Rows[0]["结算标志"].ToString() == "S")
            {
                text += "结算的订单包含：";
                for (int i = 0; i < this._dsPayment.Tables[1].Rows.Count; i++)
                {
                    text = text + this._dsPayment.Tables[1].Rows[i]["订单号"].ToString() + ";";
                }
                text += "\r\n结算的付款单包含：";
                for (int i = 0; i < this._dsPayment.Tables[2].Rows.Count; i++)
                {
                    text = text + this._dsPayment.Tables[2].Rows[i]["编号"].ToString() + ";";
                }
            }
            int num = -10;
            string appSettings = AppSettings.GetAppSettings("companyName");
            e.Graphics.DrawString(appSettings + "收据", new Font("宋体", 16f, FontStyle.Regular), Brushes.Black, 270f, (float)(num + 30));
            Pen pen = new Pen(Color.Black, 1f);
            e.Graphics.DrawLine(pen, 150, num + 65, 710, num + 65);
            e.Graphics.DrawString("日期：" + this.dtpPaymentDate.Value.ToShortDateString(), new Font("宋体", 10f, FontStyle.Regular), Brushes.Black, 370f, (float)(num + 75));
            e.Graphics.DrawString("单号：" + this.txtPaymentCode.Text, new Font("宋体", 10f, FontStyle.Regular), Brushes.Black, 120f, (float)(num + 75));
            pen.Width = 2f;
            e.Graphics.DrawLine(pen, 90, num + 110, 770, num + 110);
            e.Graphics.DrawString("收 到：", new Font("宋体", 15f, FontStyle.Regular), Brushes.Black, 120f, (float)(num + 120));
            e.Graphics.DrawString(this.txtCustomer.Text, new Font("宋体", 12f, FontStyle.Regular), Brushes.Black, 222f, (float)(num + 125));
            pen.Width = 0.5f;
            e.Graphics.DrawLine(pen, 90, num + 150, 770, num + 150);
            e.Graphics.DrawString("摘 要：", new Font("宋体", 15f, FontStyle.Regular), Brushes.Black, 200f, (float)(num + 170));
            e.Graphics.DrawString(this.cbPaymentProject.Text, new Font("宋体", 15f, FontStyle.Regular), Brushes.Black, 280f, (float)(num + 170));
            e.Graphics.DrawString(this.cbWayOfPayment.Text + "：￥" + this.txtTotalValue.Text, new Font("Times New Roman", 14f, FontStyle.Bold), Brushes.Black, 500f, (float)(num + 170));
            e.Graphics.DrawString("合计人民币（大写）：" + CommonMethod.MoneyToUpper(Convert.ToDecimal(this.txtTotalValue.Text)), new Font("宋体", 15f, FontStyle.Regular), Brushes.Black, 100f, (float)(num + 210));
            pen.Width = 3f;
            e.Graphics.DrawLine(pen, 90, num + 250, 770, num + 250);
            e.Graphics.DrawString("备注：", new Font("宋体", 15f, FontStyle.Regular), Brushes.Black, 100f, (float)(num + 260));
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Near;
            stringFormat.LineAlignment = StringAlignment.Near;
            stringFormat.Trimming = StringTrimming.EllipsisWord;
            e.Graphics.DrawString((this.txtRemark.Text.Trim() == "") ? text : (this.txtRemark.Text.Trim() + "\r\n" + text), new Font("宋体", 11f, FontStyle.Regular), Brushes.Black, new Rectangle(160, num + 260, 600, 190), stringFormat);
            pen.Width = 1f;
            e.Graphics.DrawLine(pen, 90, num + 450, 770, num + 450);
            e.Graphics.DrawString("经办人：" + this._sOperatorName, new Font("宋体", 11f, FontStyle.Regular), Brushes.Black, 100f, (float)(num + 455));
            e.Graphics.DrawString("经办人签字：", new Font("宋体", 11f, FontStyle.Regular), Brushes.Black, 300f, (float)(num + 455));
        }
    }
}
